package com.winchain.chainbill.risk.utill;

import com.shell.bee.base.exception.UnCheckException;
import com.shell.bee.base.utils.SpringContextUtil;
import com.winchain.chainbill.basic.cus.po.CusRec;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.service.CbRecService;
import com.winchain.chainbill.bill.base.service.CbSndService;
import com.winchain.chainbill.bill.biz.send.vo.BillSnd;
import com.winchain.chainbill.risk.base.service.RmIntRateService;
import com.winchain.chainbill.risk.biz.rate.vo.RateVo;

public class RateUtil {
	private static RmIntRateService rateService = null;
	private static CbRecService cbRecService = null;
	private static CbSndService cbSndService = null;
	private static CusRecService cusRecService = null;

	public static String getFixedRate(String grpId, String entId, String rateCode) {
		if (rateService == null) {
			rateService = (RmIntRateService) SpringContextUtil.getBean("rmIntRateService");
		}
		RateVo rate = rateService.findRateByCode("0", rateCode, grpId, entId, entId);
		if (null == rate) {
			throw new UnCheckException("����������������������" + rateCode);
		}
		return rate.getRateValue();
	}

	public static String getFixedRateByBillId(String billId, String rateCode) {
		if (rateService == null) {
			rateService = (RmIntRateService) SpringContextUtil.getBean("rmIntRateService");
		}
		if (cbRecService == null) {
			cbRecService = (CbRecService) SpringContextUtil.getBean("cbRecService");
		}
		CbRec cbRec = (CbRec) cbRecService.selectByKey(billId);
		String entId = "";
		if (cbRec != null) {
			entId = cbRec.getFinId();
		} else {
			throw new UnCheckException("��������������������������billId��" + billId);
		}
		RateVo rate = rateService.findRateByCode("0", rateCode, cbRec.getGrpId(), entId, entId);
		if (null == rate) {
			return "";
		}
		return rate.getRateValue();
	}

	public static String getRateByCode(String rateCode, String RateTemplateBizType, String grpId, String cusId,
			String finId) {
		if (rateService == null) {
			rateService = (RmIntRateService) SpringContextUtil.getBean("rmIntRateService");
		}
		RateVo rate = rateService.findRateByCode(RateTemplateBizType, rateCode, grpId, cusId, finId);
		if (null == rate) {
			return "";
		}
		return rate.getRateValue();
	}

	public static String getOverdueRateByBillId(String billId, String rateCode, String RateTemplateBizType) {
		if (rateService == null) {
			rateService = (RmIntRateService) SpringContextUtil.getBean("rmIntRateService");
		}
		if (cbRecService == null) {
			cbRecService = (CbRecService) SpringContextUtil.getBean("cbRecService");
		}
		if (cusRecService == null) {
			cusRecService = (CusRecService) SpringContextUtil.getBean("basCusService");
		}
		CbRec cbRec = (CbRec) cbRecService.selectByKey(billId);
		CusRec platform = cusRecService.getPlatform(null);
		String entId = platform.getCusId();
		RateVo rate = rateService.findRateByCode(RateTemplateBizType, rateCode, cbRec.getGrpId(), entId, entId);
		if (null == rate) {
			return "";
		}
		return rate.getRateValue();
	}

	public static String getFinFixRate(String RateTemplateBizType, String rateCode, String entId, String grpId) {
		if (rateService == null) {
			rateService = (RmIntRateService) SpringContextUtil.getBean("rmIntRateService");
		}
		RateVo rate = rateService.findRateByCode(RateTemplateBizType, rateCode, grpId, entId, entId);
		if (null == rate) {
			return "";
		}
		return rate.getRateValue();
	}

	public static String getVariableRate(String grpId, String finId, String cusId, String rateCode) {
		if (rateService == null) {
			rateService = (RmIntRateService) SpringContextUtil.getBean("rmIntRateService");
		}
		RateVo rate = rateService.findRateByCode("1", rateCode, grpId, cusId, finId);
		if (null == rate) {
			throw new UnCheckException("����������������������" + rateCode);
		}
		return rate.getRateValue();
	}

	public static String getEntVariableRate(String grpId, String finId, String cusId, String rateCode) {
		if (rateService == null) {
			rateService = (RmIntRateService) SpringContextUtil.getBean("rmIntRateService");
		}
		RateVo rate = rateService.findRateByCode("2", rateCode, grpId, cusId, finId);
		if (null == rate) {
			throw new UnCheckException("��������������������������" + rateCode);
		}
		return rate.getRateValue();
	}

	public static String getVariableRateByBillId(String billId, String cusId, String rateCode) {
		if (rateService == null) {
			rateService = (RmIntRateService) SpringContextUtil.getBean("rmIntRateService");
		}
		if (cbRecService == null) {
			cbRecService = (CbRecService) SpringContextUtil.getBean("cbRecService");
		}
		if (cbSndService == null) {
			cbSndService = (CbSndService) SpringContextUtil.getBean("cbSndService");
		}
		CbRec cbRec = (CbRec) cbRecService.selectByKey(billId);
		String finId = "";
		String senderId = "";
		if (cbRec != null) {
			finId = cbRec.getFinId();
			senderId = cbSndService.getBillSndById(cbRec.getBillId()).getBuyerId();
		} else {
			throw new UnCheckException("��������������������������billId��" + billId);
		}
		RateVo rate = rateService.findRateByCode("1", rateCode, cbRec.getGrpId(), senderId, finId);
		if (null == rate) {
			throw new UnCheckException("����������������������" + rateCode);
		}
		return rate.getRateValue();
	}

	public static String getEntDctRate(String grpId, String finId, String cusId, String rateCode) {
		if (rateService == null) {
			rateService = (RmIntRateService) SpringContextUtil.getBean("rmIntRateService");
		}
		RateVo rate = rateService.findRateByCode("6", rateCode, grpId, cusId, finId);
		if (null == rate) {
			throw new UnCheckException("��������������������������" + rateCode);
		}
		return rate.getRateValue();
	}

	public static String getDctRate(String grpId, String finId, String cusId, String rateCode) {
		if (rateService == null) {
			rateService = (RmIntRateService) SpringContextUtil.getBean("rmIntRateService");
		}
		RateVo rate = rateService.findRateByCode("5", rateCode, grpId, cusId, finId);
		if (null == rate) {
			throw new UnCheckException("����������������������" + rateCode);
		}
		return rate.getRateValue();
	}

	public static String getInvoiceUploadLimitDays(String grpId, String finId, String cusId, String rateCode) {
		if (rateService == null) {
			rateService = (RmIntRateService) SpringContextUtil.getBean("rmIntRateService");
		}
		RateVo rate = rateService.findRateByCode("7", rateCode, grpId, cusId, finId);
		if (null == rate) {
			throw new UnCheckException("������������������������������" + rateCode);
		}
		return rate.getRateValue();
	}

	public static String getInvoiceUploadGracePeriod(String grpId, String finId, String cusId, String rateCode) {
		if (rateService == null) {
			rateService = (RmIntRateService) SpringContextUtil.getBean("rmIntRateService");
		}
		RateVo rate = rateService.findRateByCode("7", rateCode, grpId, cusId, finId);
		if (null == rate) {
			throw new UnCheckException("����������������������������" + rateCode);
		}
		return rate.getRateValue();
	}
}
